package com.ct.main;

import com.ct.dao.Employee;
import com.ct.utils.JDBCUtils;
import com.ct.utils.Keyboard;
import com.ct.utils.MemberSum;
import com.ct.utils.UpdateResult;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

public class EmployeePanel {
    static JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.ds);
    public static void main(String[] args) throws Exception {

    }

    //管理员 面板控制
    public void adminPanel() {
        Employee employee = new Employee();
        while (true) {
            Menu.rootManageUserList();
            Menu.exit();
            String s = Keyboard.keyCode();
            switch (s) {
                case "1":
                    List<Map<String, Object>> maps = jdbcTemplate.queryForList("select *from employee");
                    for (Map<String, Object> map : maps) {
                        System.out.println(map);
                    }
                    break;
                case "2":
                    System.out.println("请输入员工id");
                    String s1 = Keyboard.keyCode();
                    System.out.println("请输入员工姓名");
                    String s2 = Keyboard.keyCode();
                    System.out.println("请输入员工密码");
                    String s3 = Keyboard.keyCode();
                    System.out.println("请输入员工性别");
                    String s4 = Keyboard.keyCode();
                    System.out.println("请输入员工电话");
                    String s5 = Keyboard.keyCode();
                    System.out.println("请输入员工职位 采购员32 管理64 收银员16");
                    String s6 = Keyboard.keyCode();
                    System.out.println("请输入员工是否在职 在职1 离职2");
                    String s7 = Keyboard.keyCode();
                    employee.setId(Integer.valueOf(s1).intValue());
                    employee.setEname(s2);
                    employee.setPassword(s3);
                    employee.setSex(s4);
                    employee.setPhoneNumber(s5);
                    employee.setPosition(Integer.valueOf(s6).intValue());
                    employee.setIsEmployed(Integer.valueOf(s7).intValue());
                    int update = jdbcTemplate.update("insert into employee values (?,?,?,?,?,?,?)", new Object[]{
                            employee.getId(), employee.getEname(), employee.getPassword(),
                            employee.getSex(), employee.getPhoneNumber(), employee.getPosition(),
                            employee.getIsEmployed()
                    });

                    if (update != 0) {
                        System.out.println("添加成功");
                    } else {
                        System.out.println("添加失败");
                    }
                    break;
                case "3":
                    System.out.println("输入你要删除的员工的名字");
                    String s8 = Keyboard.keyCode();
                    System.out.println("真的删？");
                    Keyboard.keyCode();
                    jdbcTemplate.update("delete from employee where ename = ?", s8);
                    break;
                case "4":
                    List<Map<String, Object>> maps2 = jdbcTemplate.queryForList("select * from membership");
                    for (Map<String, Object> stringObjectMap : maps2) {
                        System.out.print("卡号：");
                        System.out.print(stringObjectMap.get("idNumber") + "|");
                        System.out.print("姓名：");
                        System.out.print(stringObjectMap.get("MembershipName") + "|");
                        System.out.print("积分：");
                        System.out.print(stringObjectMap.get("score") + "|");
                        System.out.print("电话号码：");
                        System.out.print(stringObjectMap.get("phoneNumber") + "|");
                        System.out.print("申请时间：");
                        System.out.print(stringObjectMap.get("offerTime") + "|");
                    }
                    break;
                case "5":
                    System.out.println("输入你要删除的用户的id");
                    String s9 = Keyboard.keyCode();
                    int update1 = jdbcTemplate.update("delete from membership where idNumber = ?", s9);
                    if (update1 == 0) {
                        System.out.println("用户不存在");
                    }
                    break;
                case "6":
                    System.out.println("请输入会员卡号");
                    String s10 = Keyboard.keyCode();
                    System.out.println("请输入会员姓名");
                    String s11 = Keyboard.keyCode();
                    System.out.println("请输入会员积分");
                    String s12 = Keyboard.keyCode();
                    System.out.println("请输入会员电话");
                    String s13 = Keyboard.keyCode();
                    Date date = new Date();
                    int update2 = jdbcTemplate.update("insert into memberShip values (?,?,?,?,?)", s10, s11, s12, s13, date);
                    if (update2 == 0) {
                        System.out.println("操作不成功");
                    } else {
                        System.out.println("操作成功");
                    }
                    break;
                case "7":
                    List<Map<String, Object>> maps3 = jdbcTemplate.queryForList("select * from itemMessage");
                    for (Map<String, Object> stringObjectMap : maps3) {
                        System.out.print("商品号：");
                        System.out.print(stringObjectMap.get("itemid") + "|");
                        System.out.print("商品名：");
                        System.out.print(stringObjectMap.get("itemname") + "|");
                        System.out.print("普通价格：");
                        System.out.print(stringObjectMap.get("generalPrice") + "|");
                        System.out.print("会员价格：");
                        System.out.print(stringObjectMap.get("MembershipPrice") + "|");
                        System.out.print("库存：");
                        System.out.println(stringObjectMap.get("stock") + "|");
                    }
                    break;
                case "8":
                    System.out.println("请输入商品id");
                    String s14 = Keyboard.keyCode();
                    System.out.println("请输入商品名");
                    String s15 = Keyboard.keyCode();
                    System.out.println("请输入普通价格");
                    String s16 = Keyboard.keyCode();
                    System.out.println("请输入会员价格");
                    String s17 = Keyboard.keyCode();
                    System.out.println("请输入库存数量");
                    String s18 = Keyboard.keyCode();
                    int update3 = jdbcTemplate.update("insert into itemMessage values (?,?,?,?,?)", s14, s15, s16, s17, s18);
                    if (update3 == 0) {
                        System.out.println("操作不成功");
                    } else {
                        System.out.println("操作成功");
                    }
                    break;
                case "9":
                    System.out.println("请输入商品名");
                    String s19 = Keyboard.keyCode();
                    int update4 = jdbcTemplate.update("delete from itemMessage where itemName=?", s19);
                    if (update4 == 0) {
                        System.out.println("操作不成功");
                    } else {
                        System.out.println("操作成功");
                    }
                    break;
                case "10":
                    List<Map<String, Object>> maps1 = jdbcTemplate.queryForList("select COUNT(*),ename from saleMessage,employee where employee.id=saleMessage.sellerid GROUP BY ename;");
                    for (Map<String, Object> stringObjectMap : maps1) {
                        System.out.print(stringObjectMap.get("ename") + "销售了");
                        System.out.println(stringObjectMap.get("COUNT(*)") + "件");
                    }
                case "11":
                    System.out.println("当日考勤信息");
                    Date date1 = new Date();
                    String s20 = (1900 + date1.getYear()) + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
                    try {
                        List<Map<String, Object>> maps4 = jdbcTemplate.queryForList("select empid,ename,status,absentTime,offTime from attend where `year+month+day`=?",s20);
                        System.out.println((1900+date1.getYear() ) + "-" + (date1.getMonth() + 1) + "-" + date1.getDate());
                        for (Map<String, Object> stringObjectMap : maps4) {
                            System.out.print("员工id："+stringObjectMap.get("empid")+"|");
                            System.out.print("员工姓名："+stringObjectMap.get("ename")+"|");
                            System.out.print("打卡状态："+stringObjectMap.get("status")+"|");
                            System.out.print("上班时间："+stringObjectMap.get("absentTime")+"|");
                            System.out.println("下班时间："+stringObjectMap.get("offTime"));
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                case"12":
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
                    String format = simpleDateFormat.format(new Date());
                    try {
                        Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap("select SUM(price) as '营业额' from saleMessage WHERE saleTime=?", format);
                        Object 营业额 = stringObjectMap.get("营业额");
                        System.out.println("今日的营业额为："+营业额.toString());

                    }catch (Exception e){
                        System.out.println("今日营业额为0");
                    }

                    break;
                case "0":
                    Main.allLogins();
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + s);
            }
        }
    }

    //采购员 面板控制
    public void sellerPanel(Employee employee) {
        while (true) {
            boolean isExist = false;
            Menu.rootPurchaseList();
            String s = Keyboard.keyCode();
            switch (s) {
                case "1":
                    String s1 = Keyboard.keyCode();
                    int update = jdbcTemplate.update("delete from itemMessage where itemid=?", s1);
                    UpdateResult.isSuccess(update);
                    break;
                case "2":
                    System.out.println("请输入商品id");
                    String s14 = Keyboard.keyCode();
                    System.out.println("请输入商品名");
                    String s15 = Keyboard.keyCode();
                    System.out.println("请输入普通价格");
                    String s16 = Keyboard.keyCode();
                    System.out.println("请输入会员价格");
                    String s17 = Keyboard.keyCode();
                    System.out.println("请输入库存数量");
                    String s18 = Keyboard.keyCode();
                    int update3 = jdbcTemplate.update("insert into itemMessage values (?,?,?,?,?)", s14, s15, s16, s17, s18);
                    UpdateResult.isSuccess(update3);
                    break;
                case "3":
                    System.out.println("请输入商品名称");
                    String s2 = Keyboard.keyCode();
                    List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from itemMessage where itemName like ?", "%" + s2 + "%");
                    ArrayList<Map<String, Object>> a = new ArrayList<>();
                    ;
                    a = (ArrayList<Map<String, Object>>) maps;
                    for (int j = 0; j <= Math.min(3, maps.size() - 1); j++) {
                        System.out.println(j + "." + "商品名称：" + maps.get(j).get("itemname") + "   库存：" + maps.get(j).get("stock"));
                    }
                    System.out.println("选择你要修改的那个商品的库存");
                    String s4 = Keyboard.keyCode();
                    System.out.println("修改为多少个");
                    String s5 = Keyboard.keyCode();
                    int u = Integer.valueOf(s4).intValue();
                    String itemid = "";
                    if (s4.charAt(0) > '3') {
                        UpdateResult.isSuccess(-1);
                    } else {
                        itemid = a.get(u).get("itemid").toString();
                        int update1 = jdbcTemplate.update("update itemMessage  set stock = ? where itemid=?", s5, itemid);
                        UpdateResult.isSuccess(update1);
                    }
                    break;
                case ("4"):
                    //采购员
                    //上班打卡
                    Attend.goToWork(employee);
                    break;
                case ("5"):
                    //下班打卡
                    Attend.goOffWork(employee);
                    break;
                case ("0"):
                    isExist = true;
                default:
                    break;
            }
            if (isExist) {
                break;
            }
        }
    }
    //收银员面板控制
    public void cashierPanel(int id,Employee employee) {
        while (true) {
            boolean isExist = false;
            Menu.rootCashierList();
            String s = Keyboard.keyCode();
            switch (s) {
                case "1":
                    boolean isMembership = false;
                    int idNumber = 0;
                    System.out.println("是否会员");
                    System.out.println("1.会员");
                    System.out.println("2.非会员");
                    System.out.println("0.退出");

                    String s1 = Keyboard.keyCode();
                    if (s1.equals("0")) {
                        isExist = true;
                        break;
                    }
                    if (s1.equals("1")) {
                        isMembership = true;
                        System.out.println("输入会员卡号");
                        String s2 = Keyboard.keyCode();
                        int i = Integer.parseInt(s2);
                        cashOperation(isMembership, i, id);
                    } else if (s1.equals("2")) {
                        cashOperation(false, 0, id);
                    }

                    break;
                case "2":
                    System.out.println("请输入会员卡号");
                    String s3 = Keyboard.keyCode();
                    Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap("select score,MembershipName from memberShip where idNumber=?", s3);
                    System.out.println(stringObjectMap.get("MembershipName") + "" + "您的积分为：");
                    Object score = stringObjectMap.get("score");
                    System.out.println(score.toString());
                    break;
                case "3":
                    System.out.println("请输入姓名");
                    String s5 = Keyboard.keyCode();
                    System.out.println("请输入会员电话");
                    String s6 = Keyboard.keyCode();
                    int update = jdbcTemplate.update("INSERT INTO memberShip (memberShipName,score,phonenumber,offertime) VALUES (?,?,?,?)", s5, "0", s6, new Date());
                    UpdateResult.isSuccess(update);
                    break;
                case("4"):
                    //上班
                    Attend.goToWork(employee);
                    break;
                case("5"):
                    //下班
                    Attend.goOffWork(employee);
                    break;
                case "0":
                    isExist = true;
                    break;
                default:
                    System.out.println("错误输入");
            }
            if (isExist)
                break;
        }
    }

    //收银界面
    public static void cashOperation(boolean isMembership, int memberIdNumber, int cashierId) {
        BigDecimal bigDecimal = new BigDecimal(0);
        while (true) {
            BigDecimal price=new BigDecimal(0);

            if (memberIdNumber > MemberSum.MAX_MEMBER_COUNT) {
                System.out.println("卡号不正确 请重新输入");
                break;
            }
            System.out.println("输入商品id 输入0结账");

            String s = Keyboard.keyCode();
            if (s.equals("0")) {
                System.out.println("总金额为" + bigDecimal.toString());
                System.out.println("结账完成");
                break;
            }
            Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap("select * from itemMessage where itemid=?", s);
            String itemname = (String) stringObjectMap.get("itemname");
            if (itemname == null || itemname.equals("")) {
                System.out.println("商品不存在 请重新输入");
                continue;
            }
            if (isMembership) {
                BigDecimal membershipPrice = (BigDecimal) stringObjectMap.get("MembershipPrice");
                System.out.println(membershipPrice.toString());
                BigDecimal add = bigDecimal.add(membershipPrice);
                price=membershipPrice;
                bigDecimal = add;
            } else {
                BigDecimal membershipPrice = (BigDecimal) stringObjectMap.get("generalPrice");
                System.out.println(membershipPrice.toString());
                BigDecimal add = bigDecimal.add(membershipPrice);
                bigDecimal = add;
                price=membershipPrice;
            }
            Date date = new Date();
            int stock = (int) stringObjectMap.get("stock");
            if (stock <= 0) {
                System.out.println("库存不足 请重新输入");
                break;
            } else {
                jdbcTemplate.update("update itemMessage set stock = stock-1 where itemId=?", s);
            }
            jdbcTemplate.update("insert into saleMessage values (?,?,?,?,?,?)", s, itemname, date, cashierId, memberIdNumber,price);
        }
    }

}
